<?php

namespace common\models\DAO;


use yii\db\ActiveRecord;
/**
 * This is the model class for table "ub_ad".
 *
 * @property string $id
 * @property integer $type
 * @property integer $positionID
 * @property string $title
 * @property string $url
 * @property string $content
 * @property string $openType
 * @property integer $status
 * @property integer $adminID
 * @property integer $createTime
 * @property integer $updateTime
 */
class Ad extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%ad}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'positionID', 'status'], 'integer'],
            [['positionID', 'title'], 'required'],
            [['title'], 'string', 'max' => 50],
            [['openType'], 'string'],
            [['url', 'content'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键id',
            'type' => '广告类型',//1:图片,2:flash
            'positionID' => '广告类型',
            'title' => '广告名称',
            'url' => '广告链接',
            'content' => '广告内容',
            'openType' => '打开类型',
            'status' => '广告状态',//1：显示，0：不显示
            'adminID' => '管理员ID',
            'createTime' => '广告时间',
            'updateTime' => '广告时间',
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [],
            'add' => ['type', 'positionID', 'title', 'url', 'content', 'openType', 'status'],
            'edit' => ['type', 'positionID', 'title', 'url', 'content', 'openType', 'status'],
        ];
    }

    public function beforeSave($insert)
    {
        if($insert){
            $this->createTime = time();
            $this->adminID = \Yii::$app->session->get('ADMIN_ID');
        }
        $this->updateTime = time();

        return parent::beforeSave($insert);
    }


    public function getAdmin(){
        return $this->hasOne(Admin::className(), ['id' => 'adminID']);
    }

    public function getAdPosition(){
        return $this->hasOne(AdPosition::className(), ['id' => 'positionID']);
    }
}
